package com.bsb.biz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.annotation.Resources;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bsb.base.common.util.JsonUtil;
import com.bsb.base.common.util.StringUtils;
import com.bsb.base.model.SysResource;
import com.bsb.base.model.SysResourceExample;
import com.bsb.base.model.SysUser;
import com.bsb.biz.service.SysResourceService;
import com.bsb.biz.service.SysUserService;
import com.bsb.biz.shiro.UserUtils;


@Controller  
public class LoginController extends BaseController{  
     @Resource
     private SysUserService sysUserService;
     @Resource
     private SysResourceService sysResourceService;
     
    //用户登录提交方法  
    @RequestMapping(value = "/login")  
    public String login(String username,String password,String rememberMe, Model model, HttpServletRequest request,HttpServletResponse response) throws Exception{  
    	 String msg = "";  
    	 Subject subject = SecurityUtils.getSubject();
    	 if(null != subject){
    		 subject.logout();
    	 }
         if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
         	return "login";
         }
         if (!subject.isAuthenticated()){
        	 UsernamePasswordToken token = new UsernamePasswordToken(username, password);  
        	 if("true".equals(rememberMe)){
        		 token.setRememberMe(true);
        	 }
        	 
        	 try {  
        		 subject.login(token);  
        		 if (subject.isAuthenticated()||subject.isRemembered()) {
        			 return "redirect:/main";  
        		 } else {  
        			 msg = "重新登录";
        			 model.addAttribute("msg", msg);
        			 return "login";  
        		 }  
        	 } catch (IncorrectCredentialsException e) {  
        		 msg = "登录密码错误. Password for account " + token.getPrincipal() + " was incorrect.";  
        	 } catch (ExcessiveAttemptsException e) {  
        		 msg = "登录失败次数过多";  
        	 } catch (LockedAccountException e) {  
        		 msg = "帐号已被锁定. The account for username " + token.getPrincipal() + " was locked.";  
        	 } catch (DisabledAccountException e) {  
        		 msg = "帐号已被禁用. The account for username " + token.getPrincipal() + " was disabled.";  
        	 } catch (ExpiredCredentialsException e) {  
        		 msg = "帐号已过期. the account for username " + token.getPrincipal() + "  was expired.";  
        	 } catch (UnknownAccountException e) {  
        		 msg = "帐号不存在. There is no user with username of " + token.getPrincipal();  
        	 } catch (UnauthorizedException e) {  
        		 msg = "您没有得到相应的授权！" + e.getMessage();  
        	 }
         }
         model.addAttribute("msg", msg);
         return "login";  
    }  
      
    @RequestMapping("/main")
	public String main(Model model){
//    	Subject subject = SecurityUtils.getSubject();
//    	SysUser user = (SysUser)subject.getPrincipal();
    	SysUser user = (SysUser)sysUserService.getUser();
    	List<SysResource> menus;
//    	if("admin".equals(account)){
//    		SysResourceExample example = new SysResourceExample();
//    		example.setOrderByClause("concat(parent_ids, id) asc");
//			List<SysResource> pms = sysResourceService.findMenusByPid(1);
//			for (SysResource sysResource : pms) {
//				if(sysResource.getType() != SysResource.ResourceType.menu) {
//	                continue;
//	            }
//				List<SysResource> cms = sysResourceService.findMenusByPid(sysResource.getId());
//				sysResource.setChildList(cms);
//			}
//			menus = pms;
//    	}else{
//    		Set<String> permissions = sysUserService.findPermissions(account);
//    		menus = sysResourceService.findMenus(permissions);
//    	}
    	SysUser currentUser = UserUtils.currentUser();
		Long firmId = currentUser.getFirmId();
		if(firmId!=null){
			model.addAttribute("firmId", firmId);
		}else {
			model.addAttribute("firmId", 10000);
		}
    	
    	menus = sysResourceService.getUserMenuList(user.getId());
        model.addAttribute("menus", menus);
		return "main";
	}
}  